package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.MenuMapper;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage, pageSize);

        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();

        if (queryString != null && queryString.trim() != "") {
            criteria.orLike("name", "%" + queryString + "%");
        }

        List<Role> list = roleMapper.selectByExample(example);

        PageInfo<Role> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Map findAll() {

        List<Permission> permissionList = permissionMapper.selectAll();
        List<Menu> menuList = menuMapper.selectAll();

        Map map = new HashMap();
        map.put("permissionData", permissionList);
        map.put("menuData", menuList);

        return map;
    }

    @Override
    public void add(Integer[] permissionIds, Integer[] menuIds, Role role) {

        roleMapper.insert(role);

        if (permissionIds != null && permissionIds.length > 0) {
            roleMapper.insertRolePermission(role.getId(), permissionIds);
        }

        if (menuIds != null && menuIds.length > 0) {
            roleMapper.insertRoleMenu(role.getId(), menuIds);
        }
    }

    @Override
    public Map findAllById(Integer id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        Integer[] permissionIds = roleMapper.selectRolePermission(id);
        Integer[] menuIds = roleMapper.selectRoleMenu(id);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("role", role);
        map.put("permissionIds", permissionIds);
        map.put("menuIds", menuIds);

        return map;
    }

    @Override
    public void edit(Integer[] permissionIds, Integer[] menuIds, Role role) {
        roleMapper.deleteRolePermission(role.getId());
        roleMapper.deleteRoleMenu(role.getId());

        roleMapper.updateByPrimaryKey(role);

        if (permissionIds != null && permissionIds.length > 0) {
            roleMapper.insertRolePermission(role.getId(), permissionIds);
        }

        if (menuIds != null && menuIds.length > 0) {
            roleMapper.insertRoleMenu(role.getId(), menuIds);
        }
    }

    @Override
    public void deleteById(Integer id) {

        Integer count = roleMapper.findCountFromUserRole(id);
        if (count > 0) {
            throw new RuntimeException(MessageConstant.ROLE_IS_QUOTED);
        }

        roleMapper.deleteRolePermission(id);
        roleMapper.deleteRoleMenu(id);
        roleMapper.deleteByPrimaryKey(id);
    }
}
